package com.soyotec.algorithm.util;
import java.util.Random;

public class RandomGenerator  implements PseudoRandomGenerator {
    private Random rnd;
    private long seed;
    private static final String NAME = "JavaRandomGenerator";

    public RandomGenerator() {
        this(System.currentTimeMillis());
    }

    public RandomGenerator(long seed) {
        this.seed = seed;
        this.rnd = new Random(seed);
    }

    public long getSeed() {
        return this.seed;
    }

    public int nextInt(int lowerBound, int upperBound) {
        if(lowerBound < 0 && upperBound > 0){
            int seed1=0-lowerBound;
            int seed1Resutl=this.rnd.nextInt(seed1);
            int pResult=this.rnd.nextInt(upperBound);
            int result=lowerBound+seed1Resutl;
            result=result+pResult;
            if(result<Integer.MAX_VALUE){
                result+=this.rnd.nextInt(2);
            }
            if(result<Integer.MAX_VALUE){
                result+=this.rnd.nextInt(2);
            }
            return result;
        }else if(lowerBound == 0 && upperBound == Integer.MAX_VALUE){
            return this.rnd.nextInt(Integer.MAX_VALUE);
        }else if(lowerBound == -Integer.MAX_VALUE && upperBound == 0){
            return -this.rnd.nextInt(Integer.MAX_VALUE);
        }else{
            return lowerBound + this.rnd.nextInt(upperBound - lowerBound + 1);
        }

    }

    public double nextDouble(double lowerBound, double upperBound) {
        if(lowerBound<0&&upperBound>0) {
            double result= lowerBound + this.rnd.nextDouble() * (0 - lowerBound);
            return result+=this.rnd.nextDouble() * upperBound;
        }else {
            return lowerBound + this.rnd.nextDouble() * (upperBound - lowerBound);
        }
    }

    public double nextDouble() {
        return this.nextDouble(0.0D, 1.0D);
    }

    public void setSeed(long seed) {
        this.seed = seed;
        this.rnd.setSeed(seed);
    }

    public String getName() {
        return "RandomGenerator";
    }
}
